package com.hxh.generater.springBoot.genBase;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.entity.ProjectDatasource;
import com.hxh.generater.springBoot.util.ConnetMysql;
import com.hxh.util.MysqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.sql.Connection;
import java.sql.Statement;
import java.util.List;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater.util
 * @ClassName: GetAllTables
 * @Author: HXH
 * @Description: 初始化表和数据
 * @Date: 2023/11/30 9:28
 */
@Slf4j
public abstract class InitSql {

    public void createTablesAndInitData(Project project) {
        ProjectDatasource projectDatasource = project.getProjectDatasource();
        String templateName = project.getTemplate();
        try {
            createTables(projectDatasource, templateName);
            initData(projectDatasource, templateName);
            addition(project);
        } catch (Exception e) {
            log.error(templateName + "模板：初始化表和数据异常！", e);
        }

    }

    public abstract void addition(Project project);

    /**
     * @param projectDatasource: 数据源
     * @return void
     * @author HXH
     * @description: 执行初始化项目的建表语句
     * @date 2024/5/21
     **/
    private void createTables(ProjectDatasource projectDatasource, String templateName) throws Exception {
        if (StringUtils.isBlank(templateName)) {
            log.warn("跳过项目建表初始化！");
            return;
        }
        ConnetMysql connetMysql = new ConnetMysql();
        Connection connection = connetMysql.getConnection(projectDatasource);
        Statement statement = connection.createStatement();
        List<String> createTableSqlList = MysqlUtils.splitSql(Constants.templatePathPrefix + templateName + "\\template\\sql\\create_table.sql");
        if (CollectionUtils.isEmpty(createTableSqlList)) {
            log.warn("跳过项目建表初始化！");
            return;
        }
        for (String s : createTableSqlList) {
            try {
                statement.execute(s);
            } catch (Exception ignored) {
            }
        }
        statement.close();
        connection.close();
    }

    /**
     * @param projectDatasource: 数据源
     * @return void
     * @author HXH
     * @description: 给项目插入必要的数据
     * @date 2024/5/21
     **/
    private void initData(ProjectDatasource projectDatasource, String templateName) throws Exception {
        if (StringUtils.isBlank(templateName)) {
            log.warn("跳过项目数据初始化！");
            return;
        }
        ConnetMysql connetMysql = new ConnetMysql();
        Connection connection = connetMysql.getConnection(projectDatasource);
        Statement statement = connection.createStatement();
        List<String> initDataSqlList = MysqlUtils.splitSql(Constants.templatePathPrefix + templateName + "\\template\\sql\\insert_init_data.sql");
        if (CollectionUtils.isEmpty(initDataSqlList)) {
            log.warn("跳过项目数据初始化！");
            return;
        }
        for (String s : initDataSqlList) {
            try {
                statement.execute(s);
            } catch (Exception ignored) {
            }
        }
        statement.close();
        connection.close();
    }

}
